
(function(window, angular, undefined){

	var component = angular.module('water.common.component.comDatebox',['water.common.dateUtil']);
	
	/**
	 * 日期组件
	 */
	component.directive("comDatebox", function($dateUtil) {
		return {
			replace : true,
			restrict : 'E',
			template : "<input></input>",
			require : "ngModel",
			link : function(scope, element, attrs, ctrl) {
				var showCurDay = attrs.showCurday;
				var addDays=attrs.addDays;
				var options = {
					editable : false
				};
				options['onSelect'] = function(date) {
					scope.$apply(function() {
						ctrl.$setViewValue($dateUtil.formatDate(date,'yyyy-MM-dd'));
					});
				};
				var db = element.datebox(options);

				ctrl.$render = function() {
					if(typeof(ctrl.$viewValue) == "undefined" && showCurDay == "true"){
						var date = new Date();
						if(addDays!=null && addDays!="undefined" && addDays!=""){
							var day=date.getDate();
							var dayTime="";
							dayTime=Number(day)+Number(addDays);
							date.setDate(dayTime);
						}
			       		element.datebox('setValue', $dateUtil.formatDate(date));
			       		ctrl.$setViewValue($dateUtil.formatDate(date,'yyyy-MM-dd'));
					}else{
						element.datebox('setValue', ctrl.$viewValue);
					}
				};
			}
		};
	});
}(window, angular));